package com.ruoyi.mp.service.impl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.db.domain.web.MpClientBank;
import com.ruoyi.db.domain.web.dto.MpBank;
import com.ruoyi.db.mapper.web.MpClientBankMapper;

import com.ruoyi.mp.service.IMpClientBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 用户银行卡Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-01
 */
@Service
@Transactional(rollbackOn = Exception.class)
public class MpClientBankServiceImpl implements IMpClientBankService
{
    @Autowired
    private MpClientBankMapper mpClientBankMapper;

    /**
     * 查询用户银行卡
     * 
     * @param id 用户银行卡主键
     * @return 用户银行卡
     */
    @Override
    public MpClientBank selectMpClientBankById(Long id)
    {
        return mpClientBankMapper.selectMpClientBankById(id);
    }

    /**
     * 查询用户银行卡列表
     * 
     * @param mpClientBank 用户银行卡
     * @return 用户银行卡
     */
    @Override
    public List<MpClientBank> selectMpClientBankList(MpClientBank mpClientBank)
    {
        return mpClientBankMapper.selectMpClientBankList(mpClientBank);
    }

    /**
     * 新增用户银行卡
     * 
     * @param mpClientBank 用户银行卡
     * @return 结果
     */
    @Override
    public int insertMpClientBank(MpClientBank mpClientBank,String mobile)
    {
        MpClientBank idS = new MpClientBank();
        idS.setIdNumber(mpClientBank.getIdNumber());
        idS.setClientId(mpClientBank.getClientId());
        List<MpClientBank> my = mpClientBankMapper.selectMpClientBankList(idS);
        if(my!=null&&my.size()>0){
            throw new ServiceException(mpClientBank.getIdNumber()+"账户已绑定");
        }
        mpClientBank.setIsDel(0);
        mpClientBank.setCreateTime(DateUtils.getNowDate());
        List<String> bankNums = mpClientBank.getBankNumbers();
        for (String num:bankNums){
            MpClientBank bank = new MpClientBank();
            bank.setBankNumber(num);
            List<MpClientBank> list  = mpClientBankMapper.selectMpClientBankList(bank);
            if(list!=null&&list.size()>0){
                throw new ServiceException(num+"卡号已绑定");
            }
            mpClientBank.setId(null);
            mpClientBank.setBankNumber(num);
            mpClientBankMapper.insertMpClientBank(mpClientBank);
        }
        return 1;
    }

    /**
     * 修改用户银行卡
     * 
     * @param mpClientBank 用户银行卡
     * @return 结果
     */
    @Override
    public int updateMpClientBank(MpClientBank mpClientBank)
    {
        List<MpBank> bankNums = mpClientBank.getBanks();
        for (MpBank num:bankNums){
            if(num.getId()!=null){
                if(StringUtils.isNotBlank(num.getBankNumber()))
                {
                    MpClientBank bank = new MpClientBank();
                    bank.setId(num.getId());
                    bank.setBankNumber(num.getBankNumber());
                    bank.setClientId(mpClientBank.getClientId());
                    List<MpClientBank> list  = mpClientBankMapper.selectMpClientBankListNotMe(bank);
                    if(list!=null&&list.size()>0){
                        throw new ServiceException(num.getBankNumber()+"卡号已绑定");
                    }
                }
                MpClientBank clientBank = new MpClientBank();
                clientBank.setId(num.getId());
                clientBank.setBankNumber(num.getBankNumber());
                clientBank.setBankAccountName(mpClientBank.getBankAccountName());
                clientBank.setClientId(mpClientBank.getClientId());
                clientBank.setIdNumber(mpClientBank.getIdNumber());
                clientBank.setBankMobile(mpClientBank.getBankMobile());
                mpClientBankMapper.updateMpClientBank(clientBank);
            }else {
                if(StringUtils.isBlank(num.getBankNumber())){
                    continue;
                }
                MpClientBank bank = new MpClientBank();
                bank.setBankNumber(num.getBankNumber());
                bank.setClientId(mpClientBank.getClientId());
                List<MpClientBank> list  = mpClientBankMapper.selectMpClientBankList(bank);
                if(list!=null&&list.size()>0){
                    throw new ServiceException(mpClientBank.getBankNumber()+"卡号已绑定");
                }
                MpClientBank clientBank = new MpClientBank();
                clientBank.setBankNumber(num.getBankNumber());
                clientBank.setBankAccountName(mpClientBank.getBankAccountName());
                clientBank.setClientId(mpClientBank.getClientId());
                clientBank.setIdNumber(mpClientBank.getIdNumber());
                clientBank.setBankMobile(mpClientBank.getBankMobile());
                mpClientBankMapper.insertMpClientBank(clientBank);
            }
        }
        return 1;
    }

    /**
     * 批量删除用户银行卡
     * 
     * @param ids 需要删除的用户银行卡主键
     * @return 结果
     */
    @Override
    public int deleteMpClientBankByIds(Long[] ids)
    {
        return mpClientBankMapper.deleteMpClientBankByIds(ids);
    }

    /**
     * 删除用户银行卡信息
     * 
     * @param id 用户银行卡主键
     * @return 结果
     */
    @Override
    public int deleteMpClientBankById(Long id,Long clientId)
    {
        MpClientBank clientBank =  mpClientBankMapper.selectMpClientBankById(id);
        MpClientBank clientBank1 = new MpClientBank();
        clientBank1.setIdNumber(clientBank.getIdNumber());
        clientBank1.setClientId(clientId);
        return mpClientBankMapper.delAllMpClientBank(clientBank1);
    }
}
